package com.dmw.controller;

import com.dmw.dto.Dto;
import com.dmw.service.ConsumerOrderService;
import com.dmw.util.EmptyUtils;
import com.dmw.vo.orderVO.OrderInfoInVo;
import com.dmw.vo.placeOrderVO.AddOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.Month;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@RestController
@RequestMapping("/order/api/v")
public class ConsumerOrderController {

    @Autowired
    private ConsumerOrderService consumerOrderService;

    /**
     * 根据订单状态、订单创建时间、关键词(商品名称，订单号，商品编号模糊)
     * @param vo
     * @return
     */
    @PostMapping("/queryOrderList")
    @ResponseBody
    public Dto queryOrderList(@RequestBody OrderInfoInVo vo, HttpServletRequest request){
        System.out.println(">>>根据订单状态、订单创建时间、关键词(商品名称，订单号，商品编号模糊)");
        System.out.println(vo.toString());
        String token = request.getHeader("token");
        Map<String, Object> map=new HashMap<>();
        map.put("orderType",vo.getOrderType());
        map.put("orderTime",vo.getOrderTime());
        map.put("keyword",vo.getKeyword());
        String[] s = token.split("-");
        Long userId=Long.valueOf(s[2]);
        map.put("userId",userId);
        return consumerOrderService.queryOrderList(map);
    }

    /**
     * 下单接口
     * @param vo
     * @return
     */
    @PostMapping("/createOrder")
    @ResponseBody
    public Dto createOrder(@RequestBody AddOrderVO vo,HttpServletRequest request){
        System.out.println(">>>下单接口");
        System.out.println(vo.toString());
        String token = request.getHeader("token");
        System.out.println(token);
        vo.setToken(token);
        return consumerOrderService.createOrder(vo);
    }

    /**
     * 根据订单号查询订单
     * @param map
     * @return
     */
    @PostMapping("/queryOrderByOrderNo")
    @ResponseBody
    public Dto queryOrderByOrderNo(@RequestBody Map<String, Object> map){
        String orderNo = null;
        if (!EmptyUtils.isEmpty(map.get("orderNo"))){
            orderNo = map.get("orderNo").toString();
        }
        System.out.println(orderNo);
        System.out.println(">>>根据订单号查询订单");
        System.out.println(map.get("orderNo"));

        return consumerOrderService.queryOrderByOrderNo(map);
    }

    /**
     * 监听订单支付状态接口
     * @param map
     * @return
     */
    @PostMapping("/queryOrderState")
    public Dto queryOrderState(@RequestBody Map<String,Object> map){
        System.out.println(">>>监听订单支付状态接口");
        System.out.println(map.get("orderNo"));
        return consumerOrderService.queryOrderState(map);
    }
}
